<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
     String  apiId = request.getParameter("apiId");
     String  apiName = request.getParameter("apiName");
     apiName = new String(apiName.getBytes("ISO8859-1"),"UTF-8");
 %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<c:set var="apiId" value="<%=apiId %>" />
<c:set var="apiName" value="<%=apiName %>" />
<script type="text/javascript" src="${ctx }/static/js/jquery.min.js"></script>
<script type="text/javascript" src="${ctx }/static/layer/layer.js"></script>
<script type="text/javascript" src="${ctx }/static/js/json2.js"></script>
<script type="text/javascript">
$(function(){
		 	$(":button").click(function(){
		 		var url = $("form").attr("action");
		 		var param = $("form").serialize();
		 		$.post(url, param, function(result){
  					var serverCostTime = result.serverCostTime;
		 			recordApiLog(serverCostTime);
		 			//删除serverCostTime对象
		 			delete result["serverCostTime"];
  					$("#result").html(JSON.stringify(result,null,'\t'));
				},"json");
		 	});
		 	
		 	var startTime = null;
		 	var endTime = null;
		 	var apiId = ${apiId};
		 	var apiName = "${apiName}";
		 	var apiUrl = $("input[name='_url']").val();
		 	
		 	//ajax请求开始
		 	 $("#msg").ajaxStart(function(){
  			 	startTime = new Date().getTime();
 			});
 			
 			//ajax请求结束
 			$("#msg").ajaxStop(function(){
   				//do nothing
 			});
 			
 			//ajax请求出错
		 	$("#msg").ajaxError(function(event,request,settings,error){
		 		layer.msg("ajax请求出错:status:" +request.status+",readyState:"+request.readyState+",error:"+error);
			});
			
			//ajax请求成功
		 	$("#msg").ajaxSuccess(function(event, request, settings){
   				//do nothing
			 });
			 
			 function recordApiLog(serverCostTime){
			 	endTime = new Date().getTime();
		 		var totalCostTime = endTime-startTime;
		 		layer.msg("请求处理完成，总共耗时"+totalCostTime+"ms，AJAX耗时"+(totalCostTime-serverCostTime)+"ms，服务器耗时"+serverCostTime+"ms");
   				var url = "${ctx}/servlet/record-api-log";
   				var ajaxCostTime = totalCostTime-serverCostTime;
		 		var param = {"apiId":apiId,"apiName":apiName,"apiUrl":apiUrl,"totalCostTime":totalCostTime,"serverCostTime":serverCostTime,"ajaxCostTime":ajaxCostTime};
		 		$.post(url, param, function(result){
  					//$("#result").html(JSON.stringify(result,null,'\t'));
				},"json");
			 }
			 
 });		 
</script>
<div id="msg"></div>
<style type="text/css">
table{width: 100%;border-collapse: collapse;}
td{border: 1px solid black;}
._url{width: 100%;border: none;background-color: white;}
#result{width: 100%;height: 300px;}
</style>